package edu.gatech.cs2340.group7.wheresmystuffapp.model;

public class ItemLocation implements Comparable<ItemLocation> {
	private int latitude;
	private int longitude;
	public static int COMPARE_DISTANCE = 10; // distance to compare for matches (units: miles)

	/**
	 * Represents a coordinate pair of a location
	 * @param latitude2
	 * @param longitude2
	 */
	public ItemLocation(double latitude2, double longitude2) {
		latitude = (int)(latitude2 * 1E6);
		longitude = (int)(longitude2 * 1E6);
	}

	public ItemLocation(String location) {

	}

	public int getLatitude() {
		return latitude;
	}

	public int getLongitude() {
		return longitude;
	}

	@Override
	public int compareTo(ItemLocation a) {
		int dist = -1;
		if(a != null) {
			dist = (int) findDistance(latitude, longitude, a.getLatitude(), a.getLongitude());
		}
		return dist;
	}

	/**
	 * Checks to see if the two positions are the same.
	 * @param a New position to compare to
	 * @return True if the two items have the same coordinates and false otherwise
	 */
	public boolean equals(ItemLocation a) {
		boolean out = false;

		if (a != null) {
			if (latitude == a.getLatitude() && longitude == a.getLongitude()) {
				out = true;
			}
		}

		return out;
	}

	/**
	 * Calculates the distance between two positions
	 * @param latA Latitude of the first position
	 * @param lonA Longitude of the first position
	 * @param latB Latitude of the second position
	 * @param lonB Longitude of the second position
	 * @return The distance between the two points in miles
	 */
	public double findDistance(double latA, double lonA, double latB, double lonB) {
		double earthRadius = 3958.75;
		double dLat = Math.toRadians(latB - latA);
		double dLng = Math.toRadians(lonB - lonA);
		double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
				+ Math.cos(Math.toRadians(latA))
				* Math.cos(Math.toRadians(latB)) * Math.sin(dLng / 2)
				* Math.sin(dLng / 2);

		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		double dist = earthRadius * c;

		return dist;
	}
}
